package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.ServerResponse;
import service.IOrderService;
import service.impl.OrderServiceImpl;
import util.myUtil;

@WebServlet("/order")

public class OrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public OrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		myUtil.setHead(request, response);
		String type=request.getParameter("type");
		System.out.println("type:"+type);
		if("create".equals(type)) {
			create(request,response);
		}
		else if("get_order_cart_product".equals(type)) {
			get_order_cart_product(request,response);
		}
		else if("list".equals(type)) {
			list(request,response);
		}
		else if("detail".equals(type)) {
			detail(request,response);
		}
		else if("cancel".equals(type)) {
			cancel(request,response);
		}
	}
	
	/****************2.1创建订单******************/
	protected void create(HttpServletRequest request, HttpServletResponse response) {
		String address_id = request.getParameter("address_id");
		//从session中获取user_id
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		//执行创建订单逻辑
		IOrderService orderService = new OrderServiceImpl();
		ServerResponse se = orderService.create(obj_session_user_id,address_id);
		//将ServerResponse转为json并通过流写出
		myUtil.getjson(se,response);
	}
	

	//取消订单
	private void cancel(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		String order_no=request.getParameter("order_no"); 
		IOrderService orderService=new OrderServiceImpl();
		ServerResponse se = orderService.cancel(order_no,obj_session_user_id);
		
		myUtil.getjson(se,response);
	}

	//订单详情detail
	private void detail(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		String order_no=request.getParameter("order_no"); 
		IOrderService orderService=new OrderServiceImpl();
		ServerResponse se = orderService.detail(obj_session_user_id,order_no);
		
		myUtil.getjson(se,response);
	}

	//订单List
	private void list(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		String pageSize=request.getParameter("pageSize"); 
		String pageNum=request.getParameter("pageNum"); 
		IOrderService orderService=new OrderServiceImpl();
		ServerResponse se = orderService.list(pageSize,pageNum,obj_session_user_id);
		
		myUtil.getjson(se,response);
	}

	//获取订单的商品信息
	private void get_order_cart_product(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Object obj_session_user_id = session.getAttribute("id");
		String order_no=request.getParameter("order_no"); 
		IOrderService orderService=new OrderServiceImpl();
		ServerResponse se = orderService.get_order_cart_product(obj_session_user_id,order_no);
		
		myUtil.getjson(se,response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
